using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace FShaderInspector
{
    /// <summary> 本地化配置编辑器代码 </summary>
    public class LocalizeConfigMenu : EditorWindow
    {
        /// <summary> 配置文件的路径 </summary>
        public const string LocalizeConfigKey = "FSHADER_LOCALIZATION_CONFIGURATION";

        /// <summary> 配置 </summary>
        string _configuration;

        /// <summary> 滚动视图位置 </summary>
        Vector2 _scrollViewPosition;

        [MenuItem("Window/FTools/Shader Inspector/本地化配置", false, 2000)]
        static void OpenWindow()
        {
            GetWindow<LocalizeConfigMenu>("ShaderGUI 本地化配置").Show();
        }

        void OnEnable()
        {
            _configuration = EditorPrefs.GetString(LocalizeConfigKey);
        }

        void OnGUI()
        {
            minSize = new Vector2(700, 350);

            EditorGUILayout.HelpBox("配置方式为 CSV 结构: Shader属性名称,本地化名称", MessageType.Info);
            var textAreaStyle = EditorStyles.textArea.SetStretchHeight(true);
            _scrollViewPosition = EditorGUILayout.BeginScrollView(_scrollViewPosition);
            _configuration = EditorGUILayout.TextArea(_configuration, textAreaStyle);
            EditorGUILayout.EndScrollView();

            if (GUILayout.Button("保存", GUILayout.Height(30)))
            {
                EditorPrefs.SetString(LocalizeConfigKey, _configuration);
                Localization.Refresh();
                FShaderGUI.LogMessage("本地化配置保存。");
            }
        }
    }

    /// <summary> 本地化 </summary>
    public static class Localization
    {
        /// <summary> 本地化映射类 </summary>
        class PropertyForm
        {
            /// <summary> 属性名称 </summary>
            public string propertyName;

            /// <summary> 本地化名称 </summary>
            public string localizeName;
        }

        /// <summary> 本地化开启存储 Key </summary>
        const string LocalizeKey = "FSHADERGUI_LOCALIZE";

        /// <summary> 本地化映射类列表 </summary>
        static List<PropertyForm> _propertyForms
        {
            get
            {
                if (mPropertyForm != null) return mPropertyForm;

                var configurationStr = EditorPrefs.GetString(LocalizeConfigMenu.LocalizeConfigKey);
                var rows = configurationStr.Split('\n');
                mPropertyForm = new List<PropertyForm>();
                foreach (var row in rows)
                {
                    if (string.IsNullOrEmpty(row)) continue;

                    var columns = row.Trim().Split(',');
                    if (columns.Length != 2) continue;

                    var form = new PropertyForm();
                    form.propertyName = columns[0].Trim();
                    form.localizeName = columns[1].Trim();
                    mPropertyForm.Add(form);
                }

                return mPropertyForm;
            }
        }
        static List<PropertyForm> mPropertyForm;

        /// <summary> 获取本地化名称 </summary>
        /// <param name="propName">属性名称</param> <returns>本地化名称</returns>
        public static string FindName(string propName)
        {
            if (!EditorPrefs.GetBool(LocalizeKey))
                return propName;

            foreach (var form in _propertyForms)
            {
                if (form.propertyName == propName)
                    return form.localizeName;
            }
            return propName;
        }

        /// <summary> 获取本地化状态 </summary>
        /// <returns>状态</returns>
        public static bool GetLocalize() => EditorPrefs.GetBool(LocalizeKey);

        /// <summary> 设置本地化状态 </summary>
        /// <param name="state">设置状态</param> <returns>状态</returns>
        public static void SetLocalize(bool state) => EditorPrefs.SetBool(LocalizeKey, state);

        /// <summary> 刷新本地化配置 </summary>
        public static void Refresh() => mPropertyForm = null;
    }
}